/*-----------------------------------------------------------------------------
	The contents of this file are subject to the Mozilla Public License
	Version 1.1 (the "License"); you may not use this file except in
	compliance with the License. You may obtain a copy of the License at
	http://www.mozilla.org/MPL/
	
	Software distributed under the License is distributed on an "AS IS"
	basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
	License for the specific language governing rights and limitations
	under the License.
	
	The Original Code is Fever Framework code.
	
	The Initial Developer of the Original Code is Romain Ecarnot.
	Portions created by Initial Developer are Copyright (C) 2006
	the Initial Developer. All Rights Reserved.
	
	Contributor(s):
	
	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
-----------------------------------------------------------------------------*/

import fever.display.FilterBuilder;
import fever.display.ScreenShot;
import fever.Fever;
import fever.utils.Stringifier;

import flash.filters.BlurFilter;

/**
 * {@code ModalScreen } class.
 * 
 * <p>Example with <strong>AsWing component</strong>
 * {@code
 *   var modal : ModalScreen = new ModalScreen();
 *   var frame : JFrame = new JFrame( _modal.getModalTarget(), "Dialog", true );
 * }
 * 
 * @see fever.display.ScreenShot
 * 	
 * @author Romain Ecarnot
 */
class fever.display.ModalScreen
{
	//-------------------------------------------------------------------------
	// Private properties
	//-------------------------------------------------------------------------
	
	private var _container : MovieClip;	private var _topContainer : MovieClip;
	
	private var _shoot : ScreenShot;
	private var _shootContainer : MovieClip;
	private var _fillClip : MovieClip;
	
	private var _color : Number;
	private var _alpha : Number;
	
	private var _shotEnabled : Boolean;	private var _isShown : Boolean;
	
	private var _blurFilter : BlurFilter;
	private var _blurEffect : Boolean;
	
	
	//-------------------------------------------------------------------------
	// Public Properties
	//-------------------------------------------------------------------------
	
	/** Default color used to fill the screen. */
	public static var MODAL_COLOR : Number = 0x000000;
	
	/** Default alpha value of the screen clip. */
	public static var MODAL_ALPHA : Number = 10;
	
	/**
	 * Indicates if a blur effect is apply on screen.s
	 */
	public function get enableBlurEffect() { return _blurEffect; }
	public function set enableBlurEffect( b : Boolean ) : Void
	{ 
		if( b != undefined && ( b != _blurEffect ) )
		{
			_blurEffect = b;
			
			if( !_blurEffect )
			{
				_shotEnabled =  false;
				_shoot.dispose();
				FilterBuilder.removeFilter( _shootContainer, _blurFilter );
			}
			else
			{
				_shotEnabled =  true;
				_blurFilter = FilterBuilder.setBlur( _shootContainer );
			}
			if( isShown() ) update();
		}
	}
	
	/**
	 * Indicates if modal use a screen snapshot as background.<br />
	 * Default is {@code false}
	 */
	public function get enableScreenshot() : Boolean { return _shotEnabled; }
	public function set enableScreenshot( b : Boolean ) : Void
	{
		if( b != _shotEnabled )
		{
			_shotEnabled = b;
			if( isShown() ) update();
		}	
	}
	
	
	//-------------------------------------------------------------------------
	// Public API
	//-------------------------------------------------------------------------
	
	/**
	 * Constructor.
	 * 
	 * @param color Fill color of the screen
	 * @param alpha Alpha value of the screen
	 */
	public function ModalScreen( color : Number, alpha : Number )
	{
		_container = Fever.stage.createOverlayTarget();
		_topContainer = Fever.stage.createOverlayTarget();
		
		_container.onRelease = function(){};
		_container.useHandCursor = false;
		
		_shootContainer = _container.createEmptyMovieClip( '__shot__', 1 );
		_fillClip = _container.createEmptyMovieClip( '__drawer__', 2 );
		
		_blurEffect = false;
		
		hide();
		
		_color = ( color != undefined ) ? color : MODAL_COLOR;
		_alpha = ( alpha != undefined ) ? alpha : MODAL_ALPHA; 
		
		_shoot = new ScreenShot();
		_shotEnabled = false;
	}
	
	/**
	 * Returns MovieClip available target to use with this modal screen.
	 */
	public function getModalTarget() : MovieClip
	{
		return _topContainer;
	}
	
	/**
	 * Returns attached {@link fever.display.ScreenShot} instance.
	 */
	public function getScreenShot() : ScreenShot
	{
		return _shoot;	
	}
	
	/**
	 * Defines background color.
	 */
	public function setColor( color : Number ) : Void
	{
		if( color != undefined && color != _color ) _color = color;
	}
	
	/**
	 * Returns background color.
	 */
	public function getColor() : Number
	{
		return _color;	
	}
	
	/**
	 * Defines background alpha value.
	 */
	public function setAlpha( alpha : Number ) : Void
	{
		if( alpha != undefined &&  alpha != _alpha ) _alpha = alpha;
	}
	
	/**
	 * Returns background alpha value.
	 */
	public function getAlpha() : Number
	{
		return _alpha;	
	}
	
	/**
	 * Show screen.
	 * 
	 * <p>Screen layer is automatically put on top of the 'overlay' layers
	 */
	public function show() : Void
	{
		Stage.addListener( this );
		update();
		
		_putOnTopLayer();
		
		_container._visible = true;	
		_isShown = true;
	}
	
	/**
	 * Hides screen.
	 */
	public function hide() : Void
	{
		Stage.removeListener( this );
		
		_container._visible = false;
		_shoot.dispose();
		
		_isShown = false;
	}
	
	/**
	 * Indicates if screen is currently visible.
	 */
	public function isShown() : Boolean
	{
		return 	_isShown;
	}
	
	/**
	 * Disposes all screen content.
	 */
	public function dispose() : Void
	{
		hide();
		
		_shoot.dispose();
		_shootContainer.removeMovieClip();
		_fillClip.clear();
		_container.removeMovieClip();
	}
	
	/**
	 * Updates screen
	 */
	public function update() : Void
	{
		onResize();
	}
	
	/**
	 * Returns string representation.
	 */
	public function toString() : String
	{
		return Stringifier.parse( this );
	}
	
	
	//-------------------------------------------------------------------------
	// Private implementation
	//-------------------------------------------------------------------------
	
	private function onResize() : Void
	{
		if( _shotEnabled )
		{
			_shoot.update();
			_shootContainer.attachBitmap( _shoot.getData(),1 );
			
			if( _blurEffect ) _blurFilter = FilterBuilder.setBlur( _shootContainer );
		}
		
		_fillClip.clear();
		_fillClip.beginFill( _color, _alpha );
		_fillClip.moveTo( 0, 0 );
		_fillClip.lineTo( Stage.width, 0 );
		_fillClip.lineTo( Stage.width, Stage.height );
		_fillClip.lineTo( 0, Stage.height );
		_fillClip.lineTo( 0, 0 );	
	}
	
	private function _putOnTopLayer() : Void
	{
		var parent : MovieClip = _container._parent;
		var depth : Number = parent.getNextHighestDepth();
		
		_container.swapDepths( depth );
		_topContainer.swapDepths( depth + 1 );
	}	
}